package com.example.keli.base.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.utils.PayUtil;
import com.example.keli.ui.my.adapter.GiftAdapter;
import com.example.keli.ui.my.model.GiftVO;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;


/**
 * Created by jerry on 2019/3/13.
 */

public class GiftPopWindow extends PopupWindow implements PopupWindow.OnDismissListener {
    @BindView(R.id.recycler)
    RecyclerView mRecyclerview;
    @BindView(R.id.iv_reduce)
    ImageView ivReduce;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.tv_sum)
    TextView tvSum;
    @BindView(R.id.tv_pay)
    TextView tvPay;
    private Context mContext;
    private List<GiftVO> mListData;
    private GiftAdapter mAdapter;
    private String mByUid = "";//被送人id
    private String mAaid= "";//活动id
    private int mSum = 1;//当前礼物数量
    private String mGid = "9";//礼物id


    public GiftPopWindow(Context context) {
        super(context);
        this.mContext = context;
        initPopupWindow();
        View view = View.inflate(context, R.layout.pop_gift, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        //设置popwindow的宽高，这个数字是多少就设置多少dp，注意单位是dp
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        loadDatas();
    }

    private void loadDatas() {
        mListData = new ArrayList<>();
        mAdapter = new GiftAdapter(mListData, mContext);
        mRecyclerview.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerview.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GiftVO giftVO = (GiftVO) adapter.getItem(position);
                if (giftVO != null){
                    mGid = String.valueOf(giftVO.getId());
                }
                mAdapter.setChecked(position);
                mAdapter.notifyDataSetChanged();
            }
        });
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getGift(), new SubscriberListener<BaseResponse<List<GiftVO>>>() {
            @Override
            public void onSuccess(BaseResponse<List<GiftVO>> listBaseResponse) {
                mListData.addAll(listBaseResponse.data);
                mAdapter.setChecked(0);
                mGid = String.valueOf(listBaseResponse.data.get(0).getId());
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }

    /**
     * 显示popupWindow
     */
    public void showPopupWindow(View parent, String by_uid, String aaid) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            setBackgroundAlpha(0.5f);//设置屏幕透明度
            resetData(by_uid, aaid);
            this.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } else {
            this.dismiss();
        }
    }

    private void resetData(String by_uid, String aaid){
        this.mByUid = by_uid;
        this.mAaid = aaid;
        mSum = 1;
        tvSum.setText("1");
        if (mAdapter != null){
            mAdapter.setChecked(0);
            GiftVO giftVO = mAdapter.getItem(0);
            mGid = String.valueOf(giftVO != null ? giftVO.getId() : 9);
            mAdapter.notifyDataSetChanged();
        }

    }

    private void initPopupWindow() {
        setFocusable(true);
        setOutsideTouchable(true);//点击外面窗口消失
        setBackgroundDrawable(new ColorDrawable(0));
        setAnimationStyle(R.style.GiftAnimation);
        setOnDismissListener(this);
    }

    @OnClick({R.id.iv_reduce, R.id.iv_add, R.id.tv_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_reduce:
                if (mSum > 1){
                    mSum--;
                    tvSum.setText(String.valueOf(mSum));
                }
                break;
            case R.id.iv_add:
                mSum++;
                tvSum.setText(String.valueOf(mSum));
                break;
            case R.id.tv_pay:
//                BigDecimal b1 = new BigDecimal(Double.valueOf(mPrice));
//                BigDecimal b2 = new BigDecimal(Double.valueOf(mSum));
//                double money = b1.multiply(b2).doubleValue();
                PayUtil.wechatPayGift(mContext, mGid, String.valueOf(mSum), mByUid, "h5", mAaid);
//                WechatPayActivity.startGift(mContext, mGid, String.valueOf(mSum), mByUid, "h5", mAaid);
                break;
        }
    }

    @Override
    public void onDismiss() {
        setBackgroundAlpha(1.0f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     *            屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

}
